package com.tongguan.paypos.activity.trans;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.RadioButton;

import com.tongguan.paypos.R;
import com.tongguan.paypos.base.BaseActivity;
import com.tongguan.paypos.base.MyApplication;
import com.tongguan.paypos.entity.PayType;
import com.tongguan.paypos.entity.TransInfo;
import com.tongguan.paypos.fragment.QRCodeWxFragment;
import com.tongguan.paypos.fragment.QRCodeZfbFragment;
import com.tongguan.paypos.fragment.ResultOrderDetailFragment;
import com.tongguan.paypos.interfaces.JumpAct;
import com.tongguan.paypos.utils.ToastUtils;
import com.tongguan.paypos.view.CustomAlertDialog;

import java.util.ArrayList;

public class QRCodesPay extends BaseActivity {
    private RadioButton mRadioBtnWx;
    private RadioButton mRadioBtnZfb;
    private Fragment mFragment;
    private QRCodeWxFragment mWXFragment;
    private QRCodeZfbFragment mZFBFragment;
    private Bundle mBundle;
    private boolean isWx = false;
    private boolean isZfb = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcodes_pay);
        initTile(R.string.title_qr_code_pay, true);
        mRadioBtnWx = (RadioButton) findViewById(R.id.radioBtn_wx);
        mRadioBtnZfb = (RadioButton) findViewById(R.id.radioBtn_zfb);
        mBundle = getIntent().getExtras();
        if (mBundle == null) {
            return;
        }
        initPayType();
    }

    private void initPayType() {
        //加载支付方式
        if (MyApplication.getInstance().getPayTypes() == null) {
            ToastUtils.showCustom(context, "获取支付方式失败，重新再操作");
            return;
        }
        initEvent();
        ArrayList<PayType> payTypes = MyApplication.getInstance().getPayTypes();
        for (PayType payType : payTypes) {
            if ("WX".equals(payType.getType())) {
                isWx = true;
            } else if ("ZFB".equals(payType.getType())) {
                isZfb = true;
            }
        }
        //判断是否有支付方式
        if (isWx || isZfb) {
            if (isWx) {
                mWXFragment = new QRCodeWxFragment();
                mWXFragment.setArguments(mBundle);
                mWXFragment.setJumpAct(mJumpAct);
                getSupportFragmentManager().beginTransaction().add(R.id.container, mWXFragment).commit();
                mFragment = mWXFragment;
                mRadioBtnWx.setChecked(true);
            } else if (isZfb) {
                mZFBFragment = new QRCodeZfbFragment();
                mZFBFragment.setArguments(mBundle);
                mZFBFragment.setJumpAct(mJumpAct);
                getSupportFragmentManager().beginTransaction().add(R.id.container, mZFBFragment).commit();
                mFragment = mZFBFragment;
                mRadioBtnZfb.setChecked(true);
            }
            //无支付方式 不能点击
            if (!isWx) {
                mRadioBtnWx.setEnabled(false);
            }
            if (!isZfb) {
                mRadioBtnZfb.setEnabled(false);
            }
        } else {
            ToastUtils.showCustom(context, "无支付方式");
            mRadioBtnWx.setEnabled(false);
            mRadioBtnZfb.setEnabled(false);
        }
    }

    private void initEvent() {
        mRadioBtnWx.setOnClickListener(v -> showFragmentTag(0));
        mRadioBtnZfb.setOnClickListener(v -> showFragmentTag(1));
    }

    private void showFragmentTag(int index) {
        switch (index) {
            case 0:
                if (mWXFragment == null) {
                    mWXFragment = new QRCodeWxFragment();
                    mWXFragment.setArguments(mBundle);
                    mWXFragment.setJumpAct(mJumpAct);
                }
                switchFragment(mWXFragment);
                break;
            case 1:
                if (mZFBFragment == null) {
                    mZFBFragment = new QRCodeZfbFragment();
                    mZFBFragment.setArguments(mBundle);
                    mZFBFragment.setJumpAct(mJumpAct);
                }
                switchFragment(mZFBFragment);
                break;
        }
    }

    private void switchFragment(Fragment fragment) {
        //判断当前显示的Fragment是不是切换的Fragment
        if (mFragment == null) {
            return;
        }
        if (mFragment != fragment) {
            getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
            mFragment = fragment;
        }
    }

    @Override
    public void onBackPressed() {
        new CustomAlertDialog.Builder(context).setTitle(R.string.common_tip_title)
                .setMessage("是否取消交易？")
                .setPositiveButton(R.string.common_sure, (dialog, which) -> {
                    closeFragment();
                    super.onBackPressed();
                }).setNegativeButton(R.string.common_cancel, (dialog, which) -> {
        }).show();
    }

    JumpAct mJumpAct = new JumpAct() {
        @Override
        public void successJumpAct(TransInfo info, String wxImageUrl) {
            if (mFragment!=null){
                if (mFragment == mWXFragment){
                    if (mZFBFragment != null) {
                        mZFBFragment.closeOrder();
                    }
                }else if (mFragment == mZFBFragment){
                    if (mWXFragment != null) {
                        mWXFragment.closeOrder();
                    }
                }
            }
            Bundle bundle = new Bundle();
            bundle.putParcelable(ResultOrderDetailFragment.ORDER_DATA, info);
            bundle.putString(ResultOrderDetailFragment.WX_IMG_URL, wxImageUrl);
            bundle.putString(ResultManageActivity.EXCHANGE_FRAGMENT_NAME, ResultOrderDetailFragment.class.getName());
            jumpAct(ResultManageActivity.class, bundle, false, true);
        }

        @Override
        public void onTimeOut() {
            ToastUtils.showCustom(context," 支付超时 ");
            closeFragment();
            finish();
        }
    };

    private void closeFragment(){
        if (mFragment!=null){
            if (mFragment == mWXFragment){
                mWXFragment.closeOrder();
                if (mZFBFragment != null) {
                    mZFBFragment.closeOrder();
                }
            }else if (mFragment == mZFBFragment){
                mZFBFragment.closeOrder();
                if (mWXFragment != null) {
                    mWXFragment.closeOrder();
                }
            }
        }
    }
}
